package com.jltx.zeus.arch

import android.content.Intent
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Observer
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.repeatOnLifecycle
import com.jltx.xq.zeus.startup.StartupListener
import com.jltx.xq.zeus.startup.StartupManager
import com.jltx.zeus.arch.task.*
import com.jltx.zeus.view.BaseActivity
import com.jltx.zeus.viewmodel.ext.parameter
import com.jltx.zeus.viewmodel.ext.parametersOf
import com.jltx.zeus.viewmodel.ext.viewModels
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flowWith
import kotlinx.coroutines.flow.launchIn

class MainActivity : BaseActivity() {
    private val mainViewModel by viewModels<MainViewModel> {
        parametersOf(
            parameter<String>("参数")
        )
    }

    override val layoutId: Int = R.layout.activity_main

    override fun initListener() {
        mainViewModel.toastLiveData.observe(this, Observer {
            Toast.makeText(MainActivity@ this, it, Toast.LENGTH_SHORT).show()
        })

        launchUI {
            repeatOnLifecycle(Lifecycle.State.RESUMED) {
                mainViewModel.wechatData.collect {
                    Log.d("HHHHH", "测试收到")
                }
            }
//            mainViewModel.wechatData.flowWithLifecycle(lifecycle,Lifecycle.State.RESUMED)
//                .collect{
//                    Log.d("HHHHH", "测试收到")
//                }

//            mainViewModel.wechatData.collect {
//                Log.d("HHHHH", "测试收到")
//            }
        }

        btn1.setOnClickListener {
//            mainViewModel.getToastMsg()
            StartupManager.Builder()
                .addStartup(Task2())
                .addStartup(Task4())
                .addStartup(Task5())
                .addStartup(Task3())
                .addStartup(Task1())
                .setListener(object : StartupListener{
                    override fun onCompleted() {
                        Log.d("GGGGG", "所有任务完成")
                    }

                })
                .build(this)
                .start().await()
        }

        btn2.setOnClickListener {
            mainViewModel.getToastMsgDeferred()
        }

        btn3.setOnClickListener {
            mainViewModel.getToastMsgProduce()
        }

        btn4.setOnClickListener {
            mainViewModel.getWeChatNews()
        }

        btn5.setOnClickListener {
            mainViewModel.getWeChatNewsRxJava()
        }
        btn.setOnClickListener {
//            startActivity(Intent(this, MemoryLeakActivity::class.java))
            mainViewModel.getWeChatNewsFlow()
        }
    }
}
